package tech.aistar.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import tech.aistar.entity.*;
import tech.aistar.entity.dynamic.Picture;
import tech.aistar.entity.massage.Massage;
import tech.aistar.entity.massage.MassageList;
import tech.aistar.service.*;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/mass")
public class MassageController {
    @Autowired
    private IMassageService massageService;
    @Autowired
    private IUserInfoService userInfoService;
    //帖子
    @Autowired
    private IPostService postService;
    //帖子主题
    @Autowired
    private IThemeService themeService;
    //评论
    @Autowired
    private IComService comService;
    //宠物秀
    @Autowired
    private IPictureService pictureService;


    @GetMapping("/page/{uId}")
    public String MassageIndex(Model model, HttpServletRequest req, @PathVariable("uId") Integer uId){
        HttpSession ss=req.getSession();
        UserLogin user= (UserLogin) ss.getAttribute("user");
//        Integer num = (Integer) req.getSession().getAttribute("massNum");
        ss.setAttribute("massNum",0);
        if(null==user){
            return "redirect:/user/loginding";
        }else {
            //帖子
            List<Massage> post = massageService.findUidAndPost(uId);
            List<MassageList> postMass = new ArrayList<>();
            for (Massage pm : post) {
                massageService.ViewMassage(pm.getmId());
                UserInfo userInfo = userInfoService.findById(pm.getuId());
                Post post1 = postService.findPostByPostId(pm.getPostId());
                Theme theme = themeService.findThemeById(post1.getThemeId());
//            Com com = comService


                MassageList massageList = new MassageList();
                massageList.setId(pm.getmId());
                massageList.setReId(pm.getReId());
                massageList.setReName(userInfo.getuName());
                massageList.setPicName(userInfo.getPicfilename());
                massageList.setcTime(pm.getcTime());
                massageList.setThemeName(theme.getThemeName());
                massageList.setPostId(pm.getPostId());
//            标题加内容
                massageList.setPostTital(post1.getTitle() + post1.getPostContent());
                massageList.setContent(pm.getContent());
                massageList.setCommId(null);
                //点赞的评论内容
                massageList.setCommTital(null);
                massageList.setCommAndPost(null);
                massageList.setType(null);
                postMass.add(massageList);

            }
            model.addAttribute("postMass", postMass);
            //评论
            List<Massage> comm = massageService.findUidAndCom(uId);
            List<MassageList> commMass = new ArrayList<>();
            for (Massage m : comm) {
                massageService.ViewMassage(m.getmId());
                UserInfo userInfo = userInfoService.findById(m.getuId());
                //获取这条评论
                Com com = comService.findComByComId(m.getCommitId());
                //获取帖子id 从而 获得帖子的标题
                Post post1 = postService.findPostByPostId(com.getPostId());
                Theme theme = themeService.findThemeById(post1.getThemeId());

                MassageList massageList = new MassageList();
                massageList.setId(m.getmId());
                massageList.setReId(m.getReId());
                massageList.setReName(userInfo.getuName());
                massageList.setPicName(userInfo.getPicfilename());
                massageList.setcTime(m.getcTime());
                massageList.setThemeName(theme.getThemeName());
                massageList.setPostId(null);
//            标题加内容
                massageList.setPostTital(post1.getTitle() + post1.getPostContent());
                massageList.setContent(m.getContent());
                massageList.setCommId(m.getCommitId());
                //我的这条评论的内容
                massageList.setCommTital(com.getContent());
                //点赞的评论内容
                massageList.setCommAndPost(null);
                massageList.setType(null);
                commMass.add(massageList);
            }
            model.addAttribute("commMass", commMass);

            //点赞
//        List<Massage> givePicMass = massageService.findPicZan(uId);
//        List<Massage> givePostMass = massageService.findUidZpost(uId);
//        List<Massage> giveComMass = massageService.findUidAndZCom(uId);
//        //最后的
//        List<MassageList> giveAll = new ArrayList<>();
//
//        for(Massage g:givePicMass){
//            UserInfo userInfo = userInfoService.findById(g.getuId());
//            //获取帖子id 从而 获得帖子的标题
//            Picture picture = pictureService.getByPicId(g.getPicId());
//
//            MassageList massageList = new MassageList();
//            massageList.setId(m.getmId());
//            massageList.setReId(m.getReId());
//            massageList.setReName(userInfo.getuName());
//            massageList.setPicName(userInfo.getPicfilename());
//            massageList.setcTime(m.getcTime());
//            massageList.setThemeName(theme.getThemeName());
//            massageList.setPostId(null);
////            标题加内容
//            massageList.setPostTital(post1.getTitle()+post1.getPostContent());
//            massageList.setContent(m.getContent());
//            massageList.setCommId(m.getCommitId());
//            //我的这条评论的内容
//            massageList.setCommTital(com.getContent());
//            //点赞的评论内容
//            massageList.setCommAndPost(null);
//            massageList.setType(null);
//            commMass.add(massageList);
//        }
//        model.addAttribute("giveAll",giveAll);
        }
        return "/massage/massage";
    }
}
